import { getPublicOrderList,PublicOrderReject,PublicOrderAss } from "@/apis/workOrder.js";
import { parseTime, getTotalPageNum } from "@/util";

import {
	deepClone
} from "@/util";


export default {
  data() {
    return {
      currStatus: 0,
      showStartTime: false,
      showEndTime: false,
      showPicker: false,
      currItem: null,
  
    
      kw: "",
      currPageNo: 1,
      totalPagesNo: 1,
      orderList: [],
      AssConfirmDescr: "",
      queryForm: {
        Start: "",
        End: "",
        Kw: "",
        PageNo: 1,
        PageSize: 10,
      },
      showAssReject: false,
    };
  },
  onShow() {
  },
  onLoad(){
    this.initQuery();
    this.fetchData();

  },
  methods: {
    async handleOrderAss(currItem) {
			if (!currItem) return
			const res = await PublicOrderAss({
				RepairId: currItem.RepairInfo.ID,
			})
			if (res.MsgCode != 0) return
			uni.showToast({
				title: '接单成功',
				duration: 1000,
				icon: 'success'
			});
			setTimeout(() => {
				this.initQuery()
				this.fetchData()
			}, 500)
		},
		async handleAssReject() {
			const currItem = this.currItem
			if (!currItem) return
			const res = await PublicOrderReject({
				RepairId: currItem.RepairInfo.ID,
			})
			if (res.MsgCode != 0) return
			this.showAssReject = false
			uni.showToast({
				title: '拒绝成功',
				duration: 1000,
				icon: 'success'
			});
			setTimeout(() => {
				this.initQuery()
				this.fetchData()
			}, 500)
		},
    handleSearch() {
      this.initQuery();
      this.fetchData();
    },
    initQuery() {
      this.queryForm.PageNo = 1;
      this.totalPagesNo = 1;
      this.orderList = [];
    },
    async fetchData() {
      const queryFormCopy = deepClone(this.queryForm)

      console.log('fetchData form-----', queryFormCopy)
      const res = await getPublicOrderList(queryFormCopy);
      if (res.MsgCode != 0) return;

      this.orderList = [...this.orderList, 
        ...res.data.OrderList.map(item=>{
        item.Buttons = JSON.parse(item.Buttons ? item.Buttons : '{}')
        return item
      })
      ];
      console.log('this.orderList',this.orderList)
      this.totalPagesNo = getTotalPageNum(res.data.Count, 10);
    },
    handleInfoClick(item, type) {
      console.log("handleInfoClick", item);
      if (type == "tel") {
        uni.makePhoneCall({
          phoneNumber: item.RepairInfo.Tel,
        });
        return;
      } else {
        return;
      }
    },
    handleDateConfirm(e,key) {
      console.log('handleDateConfirm', e.value, e.mode)
      setTimeout(() => {
        this.queryForm[key] = uni.$u.timeFormat(e.value, 'yyyy-mm-dd')
      }, 0);

    },
    formatImgUrl(arrStr) {
      try {
        const arr = arrStr.split(",");
        if (arr[0] == "") return;
        return arr.map((item) => {
          return "http://img.help10000.com" + item;
        });
      } catch (ex) {
        console.error("formatImgUrl", ex.message);
      }
    },
    scrolltolower() {
      console.log("scrolltolower", this.totalPagesNo, this.queryForm);
      if (this.queryForm.PageNo < this.totalPagesNo) {
        this.queryForm.PageNo += 1;
        this.fetchData();
      }
    },
  },
};
